/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actions;

import edu.ipfw.cs590.cheat.AccessRights;
import edu.ipfw.cs590.cheat.AssignmentManagementInterface;
import edu.ipfw.cs590.cheat.AssignmentManagementService;
import edu.ipfw.cs590.cheat.RegisteredPerson;
import edu.ipfw.cs590.cheat.UserRights;
import edu.ipfw.cs590.cheat.struts.actionforms.RegisterUserForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author safar
 */
public class RegisterUserAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (form instanceof RegisterUserForm) {
            //Convert the form the a register user form.
            RegisterUserForm ruf = (RegisterUserForm) form;

            try {
                // Call Web Service Operation
                AssignmentManagementService service = new AssignmentManagementService();
                AssignmentManagementInterface port = service.getAssignmentManagementSOAP();

                //Initialize the registered user
                RegisteredPerson regReq = new RegisteredPerson();
                AccessRights ar = new AccessRights();
                ar.setRights(UserRights.valueOf(ruf.getAccessType()));
                regReq.setUserId(ruf.getUserName());
                regReq.setFirstName(ruf.getFirstName());
                regReq.setLastName(ruf.getLastName());
                regReq.setAffiliation(ruf.getAffiliation());
                regReq.setPassword(ruf.getPassword());
                regReq.setEmail(ruf.getEmail());
                regReq.setPhone(ruf.getPhone());
                regReq.setRights(ar);
                regReq.setUrl(ruf.getUrl());

                //If we get a successful registration return success otherwise error
                if (port.register(regReq)) {
                    return mapping.findForward(SUCCESS);
                } else {
                    //Something bad happened on the webservice side fail.
                    ActionMessages errors = new ActionMessages();
                    errors.add(ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage("errors.service.register"));
                    this.saveErrors(request, errors);

                    return mapping.getInputForward();
                }
            } catch (Exception exc) {
                //Something bad happened on the webservice side fail.
                ActionMessages errors = new ActionMessages();
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("errors.service.error", "Register", exc.getMessage()));
                this.saveErrors(request, errors);

                return mapping.getInputForward();
            }
        }

        //Something bad happened on the webservice side fail.
        ActionMessages errors = new ActionMessages();
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.form.invalid"));
        this.saveErrors(request, errors);

        return mapping.getInputForward();
    }
}
